import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import BasicLayout from '@/layouts/BasicLayout.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import waybillRouter from './waybill'
import store from '@/store'
import newsCenterRouter from './newsCenter'
import mine from './mine'

NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

const routes = [
  {
    path: '/',
    redirect: '/delivery',
    component: BasicLayout,
    children: [
      {
        path: '/delivery',
        name: 'Delivery',
        meta: { title: '发货', showTab: true, keepAlive: true },
        component: () => import('@/views/delivery/Delivery.vue')
      },
      {
        path: '/delivery/AddressInfo',
        name: 'AddressInfo',
        meta: { title: '地址信息' },
        component: () => import('@/views/delivery/AddressInfo.vue')
      },
      {
        path: '/delivery/deliveryInfo',
        name: 'DeliveryInfo',
        meta: { title: '完善发货信息', keepAlive: true },
        component: () => import('@/views/delivery/DeliveryInfo.vue')
      },
      {
        path: '/delivery/assignOrder',
        name: 'AssignOrder',
        meta: { title: '指派司机' },
        component: () => import('@/views/delivery/AssignOrder.vue')
      },
      {
        path: '/delivery/addressList',
        name: 'AddressList',
        meta: { title: '常用地址' },
        component: () => import('@/views/delivery/AddressList.vue')
      },
      {
        path: '/waybill',
        name: 'Waybill',
        meta: {
          title: '运单',
          showTab: true,
          isUseCache: false, //设置isUseCache为true，此时返会组件页面是不会重新获取数据的，使用的缓存数据。
          keepAlive: true // 通过此字段判断是否需要缓存当前组件
        },
        component: () => import('@/views/waybill/WayBill.vue')
      },
      {
        path: '/mine',
        name: 'Mine',
        meta: { title: '我的', showTab: true },
        component: () => import('@/views/mine/Mine.vue')
      },
      {
        path: '/mine-wallet',
        name: 'MineWallet',
        component: () => import('@/views/mine/children/wallet/Wallet.vue'),
        meta: { title: '我的钱包' }
      },
      ...waybillRouter,
      ...newsCenterRouter,
      ...mine
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' },
    beforeEnter(to: any, from: any, next: any) {
      const { isLogin } = localStorage
      // const isLogin = true
      isLogin ? next({ name: 'Delivery' }) : next()
    }
  },
  {
    path: '/agreement-policy',
    name: 'AgreementPolicy',
    component: () => import('@/views/help/AgreementPolicy.vue'),
    meta: { title: '用户服务协议' }
  },
  {
    path: '/mine-privacy-agreement',
    name: 'PrivacyAgreement',
    component: () => import('@/views/help/PrivacyAgreement.vue'),
    meta: { title: '隐私政策' }
  },
  {
    path: '/help-technical-service-agreement',
    name: 'TechnicalServiceAgreement',
    component: () => import('@/views/help/TechnicalServiceAgreement.vue'),
    meta: { title: '货物运输技术服务协议' }
  },
  {
    path: '/help-transport-service-agreement',
    name: 'TransportServicesAgreement',
    component: () => import('@/views/help/TransportServicesAgreement.vue'),
    meta: { title: '货物运输协议' }
  }
]

// KeepAlive 详情页返回列表页滚动到缓存位置
function scrollBehavior(to: any, from: any, savedPosition: any) {
  if (savedPosition) {
    return savedPosition
  }
  return { left: 0, top: 0 }
}

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior
})

router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.path !== from.path) {
    NProgress.start()
  }
  if (to.meta.keepAlive) {
    store.commit('cachedView/setCachedView', to)
  }

  // 路由切换时结束之前正在等待加载数据的请求
  store.commit('http/clearToken')

  const { isLogin } = localStorage
  // const isLogin = true
  const { name } = to
  const isLoginOrRegister = name === 'Login' || name === 'AgreementPolicy' || name === 'PrivacyAgreement'
  isLogin || isLoginOrRegister ? next() : next({ name: 'Login' })
})

router.afterEach(() => {
  NProgress.done()
})

export default router
